package my.sample.exception;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class HandlingException {
	public static void main(String[] args) throws IOException {

		HandlingException handle = new HandlingException();

		int count = 0;
		int maxTries = 30;
		while (true) {
			try {
				String data = handle.getData();
				if (data != null || data.isEmpty()) {
					System.out.println("parsed data : " + data);
					break;
				}
			} catch (IOException e) {
				System.out.println("Caught : " + e.getMessage().contains("No such file or directory") + " : " + count + " - going to wait...");
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e1) {
					System.out.println("InterruptedException : " + e.getMessage());
				}
				if (++count == maxTries) {
					break;
				}
			}
		}
	}

	private String getData() throws IOException, FileNotFoundException {
		BufferedReader br = new BufferedReader(new FileReader("/tmp/io.txt"));
		String line;
		String data = "";
		while ((line = br.readLine()) != null) {
			// process the line.
			if (line.startsWith("drop.hourly.db")) {
				data = (line.split(":")[1]);
			}
		}
		br.close();
		return data;
	}
}
